package com.ai.trickycards.utils

import com.ai.trickycards.entity.enumeration.LevelRangeEnum
import com.ai.trickycards.entity.enumeration.MedalEnum
import java.math.RoundingMode
import java.text.SimpleDateFormat
import java.util.Locale
import kotlin.math.pow

object Utils {
    fun numToWord(num: Int): String {
        return when (num) {
            0 -> "Zero"
            1 -> "One"
            2 -> "Two"
            3 -> "Three"
            4 -> "Four"
            5 -> "Five"
            6 -> "Six"
            7 -> "Seven"
            8 -> "Eight"
            9 -> "Nine"
            10 -> "Ten"
            11 -> "Eleven"
            12 -> "Twelve"
            13 -> "Thirteen"
            14 -> "Fourteen"
            15 -> "Fifteen"
            16 -> "Sixteen"
            17 -> "Seventeen"
            18 -> "Eighteen"
            19 -> "Nineteen"
            20 -> "Twenty"
            21 -> "Twenty-one"
            22 -> "Twenty-two"
            23 -> "Twenty-three"
            24 -> "Twenty-four"
            else -> "$num"
        }
    }

    fun getNextPosition(
        currentIndex: Int,
        totalCount: Int
    ): Int {
        val index = currentIndex + 1
        return if (index >= totalCount) 0 else index
    }

    fun getPrePosition(
        currentIndex: Int,
        totalCount: Int
    ): Int {
        return if (currentIndex > 0) {
            currentIndex - 1
        } else {
            totalCount - 1
        }
    }

    /**
     * 获取等级对应的勋章
     */
    fun getMedal(level: Int): MedalEnum? {
        MedalEnum.entries.toList().reversed().forEach {
            if (level > it.level) {
                return it
            }
        }
        return null
    }

    /**
     * 获取等级成就
     */
    fun getLevelRange(level: Int) = LevelRangeEnum.entries.find { level >= it.startLevel && level <= it.endLevel } ?: LevelRangeEnum.P1

    /**
     * 获取等级进度
     */
    fun getLevelProgress(level: Int): Float {
        return getLevelRange(level).let {
            (level - it.startLevel) / 1f / (it.endLevel - it.startLevel + 1)
        }
    }

    /**
     * 计算有多少小时
     * @param scale 保留小数位个数
     */
    fun getHour(duration: Long, scale: Int = 1): Float {
        return (duration / 1000 / 60 / 60f).toBigDecimal().setScale(scale, RoundingMode.DOWN).toFloat()
    }

    /**
     * 去掉小数位末尾的无效0
     * @param num 需要格式化的数字
     * @param decimalPlaces 最多保留的小数位数
     */
    fun formatNum(
        num: Float,
        decimalPlaces: Int = 1
    ): String {
        val temp = 10f.pow(decimalPlaces)
        val temp2 = (num * temp).toInt() / temp
        return temp2.toString().trimEnd('0').trimEnd('.')
    }

    fun today(pattern: String = "yyyy-MM-dd"): String {
        return SimpleDateFormat(pattern, Locale.getDefault()).format(System.currentTimeMillis())
    }
}